<template>
  <div class="bh-mb-16">
    <Modal v-model="personModal" title="添加成员" :width="1000" :mask-closable="false">
			<div class="antd-pe-person person-zb" style="position:static">
				<div class="tjyh-search">
					<Input v-model="queryValue" @keyup.enter.native="searchUser" placeholder="请输入姓名/职工号" style="width: 328px" />
					<Button type="primary" @click="searchUser">搜索</Button>
				</div>
				<div class="tjyh-main">
					<antd-pe-left :tree-data="treeData" :tab-data="tabData" @on-select="treeItemSelect_p"></antd-pe-left>
					<antd-pe-center ref="antd-pe-center" :users="users" @on-check="check_p" type="radio" :result="selected" :isShowLoadmore="isShowLoadmore" @on-search-user="searchUser"></antd-pe-center>
					<antd-pe-right  v-model="selected" @on-delete="handleDelete"></antd-pe-right>
				</div>
			</div>
			<div slot="footer">
				<Button @click="personModal=false">取消</Button>
				<Button type="primary" :loading="saveLoading" @click="confirm()">确定</Button>
			</div>
		</Modal>
  </div>
</template>

<script>
import pageUtil from '../../lib/bh-vue/utils/pageUtil'
import { request } from 'utils/http'

const turing = window['tg-turing']

export default {
  props: {
    queryPath: Object,
    formUrl: String
  },
  data () {
    return {
      taskid: null,
      //选人数据
      users:[],
      tabData: [],
      selected:[],
      treeData: [],
      queryValue: '',
      isShowLoadmore: true,
      totalSize: 0,
      pageNo: 1,
      loadDatas: [],
      pe_WID: '',
      ISMINE: '',
      userGroupBread: '',
      currentGroupType: '',
      dymUserModal: false,
      maskClosable: false,
      saveLoading: false,
      personModal: false,
      appName: null
    }
  },
  computed: {
    // computeDisabled() {

    // }
  },
  methods: {
    queryOrganization(taskid, appName){
      this.appName = appName
      this.personModal = true
      this.taskid = taskid
      //查询组织机构
      const url = window.contextPath + "/sys/emapflow/modules/elecseal/deptQuery.do"
      request(url, {
        query: '',
        headers: {"Content-Type":"application/x-www-form-urlencoded", "X-Requested-With" : "XMLHttpRequest"}
      }).then(res => {
        if(res.code === "0") {
          let datas = res.datas.deptQuery.rows
          datas = turing.utils.toTreeData(datas, null, {ukey:"DM", pkey:'LS', toCKey:'children'})
          this.treeData = datas;
        }else{
          this.$Message.error(res.data.msg)
        }
      })
    },
    treeItemSelect_p(data){
      //根据树对应单元查询人员数据
      this.pageNo = 1;
      this.queryValue= '';
      this.loadDatas = [];
      var WID = data[0].DM;
      this.pe_WID = WID;
      var DM = data[0].DM;
      this.ROLEDM = DM;
      var params = {
        SZDWDM: WID,
        ROLEDM: this.ROLEDM_OUT ? this.ROLEDM_OUT:null,
        pageSize: 10,
        pageNumber: 1
      };
      var url = window.contextPath + "/sys/emapflow/modules/elecseal/jzgQuery.do";
      request(url, {
        query: params,
        headers:{"Content-Type":"application/x-www-form-urlencoded", "X-Requested-With" : "XMLHttpRequest"}
      }).then(res => {
        if(res.code === "0") {
          let datas = res.datas.jzgQuery.rows
          datas.forEach(function(ele){
            ele.deptName = ele.MC;
          })
          this.users = datas;
          if(datas.length < res.datas.jzgQuery.totalSize){
            this.isShowLoadmore = true;
          }else{
            this.isShowLoadmore = false;
          }
          this.loadDatas = datas;
        }else{
          this.$Message.error(res.data.msg)
        }
      })
    },
    check_p: function(data){
      this.selected = data;
    },
    handleDelete(data){
      var antdPeCenterVM = this.$refs['antd-pe-center'];
      if(Object.prototype.toString.call(data) === '[object Object]'){
          antdPeCenterVM.options.map(function(option){
              if(data.ZGH === option.ZGH) {
                  antdPeCenterVM.isCheckedAll = false;
                  option._isSelected = false;
              }
              return option
          });
      }else{
          // 删除全部,多选checkbox专有特性
          var arr = [];
          data.map(function(obj){
              arr.push(obj.ZGH);
          });
          antdPeCenterVM.options.map(function(option){
              if(arr.indexOf(option.ZGH)>-1) {
                  antdPeCenterVM.isCheckedAll = false;
                  option._isSelected = false;
              }
              return option
          });
          console.log(antdPeCenterVM.options)
      }
    },
    confirm: function(){
      this.saveLoading = true
      var results = this.selected;
      if(results.length == 0){
          this.$Message.error({
              content: "没有选择任何人员，请点击取消或点击弹窗右上角关闭按钮进行关闭",
              duration: 3,
          })
          this.saveLoading = false;
          return;
      }
      var ZghInfo = [];
      results.forEach(function(obj){
          ZghInfo.push(obj.ZGH)
          delete obj._isSelected;
          delete obj._disabled;
      });
      const params = {
        assignee: ZghInfo[0],
        commandType: "changeAssignee",
        taskId: this.taskid,
        appName: this.appName,
      }
      const url = window.contextPath + "/sys/emapflow/tasks/changeAssignee.do"
      request(url, {
        query: params,
        headers:{"Content-Type":"application/x-www-form-urlencoded", "X-Requested-With" : "XMLHttpRequest"}
      }).then(res => {
        if(res.succeed) {
          pageUtil.tip('转办成功', 'success')
          this.$emit("save-success")
          this.saveLoading = false;
          this.personModal = false
        }else{
          this.saveLoading = false;
          pageUtil.tip('转办失败', 'danger')
        }
      })
      
      // var params = {
      //     ROLEDM: this.ROLEDM_OUT ? this.ROLEDM_OUT:null,
      //     ZGH:JSON.stringify(ZghInfo)
      // }
      // var url = api.bs.person.actions.commonUserAdd;
      // //保存已选组织人员
      // inst.execute(url, params).then(result => {
      //     if (result.data.code === '0' && result.data.datas[url] > 0) {
      //         this.$Message.success('组织机构人员添加成功')
      //         this.$refs.grid.reload();
      //         this.personModal = false;
      //     } else {
      //         this.$Message.error(result.data.msg)
      //         this.personModal = false;
      //     }
      //     // this.saveLoading = false
      // }).catch(err => {
      //     this.$Message.error('组织机构人员添加失败')
      // })
    },
    searchUser (element){
      if(!this.queryValue && !element){
          return
      }
      // this.users = [];
      // this.selected = [];
      var params = {};
      if(element == "loadmore"){
          this.pageNo = this.pageNo+1
          params = {
              SZDWDM: this.pe_WID,
              ROLEDM: this.ROLEDM_OUT ? this.ROLEDM_OUT:null,
              pageSize: 10,
              pageNumber: this.pageNo
          };
          if(this.queryValue){
              params = {
                  NAME: this.queryValue,
                  ROLEDM: this.ROLEDM_OUT?this.ROLEDM_OUT:null,
                  pageSize: 10,
                  pageNumber: this.pageNo
              };
          }
      }else{
          this.pageNo = 1;
          this.loadDatas = [];
          //根据姓名工号查询
          params = {
              NAME: this.queryValue,
              ROLEDM: this.ROLEDM_OUT?this.ROLEDM_OUT:null,
              pageSize: 10,
              pageNumber: 1
          };
      }
      var url = window.contextPath + "/sys/emapflow/modules/elecseal/jzgQuery.do";
      if(this.personInterface && this.personInterface.cxzzry){
          url = this.personInterface.cxzzry;
      }
      request(url, {
        query: params,
        headers:{"Content-Type":"application/x-www-form-urlencoded", "X-Requested-With" : "XMLHttpRequest"}
      }).then(res => {
        if(res.code === "0") {
          let datas = res.datas.jzgQuery.rows
          datas.forEach(function(ele){
              ele.deptName = ele.MC;
          })
          this.loadDatas = this.loadDatas.concat(datas);
          if(this.loadDatas.length == res.datas.jzgQuery.totalSize){
              this.isShowLoadmore = false;
          }else if(this.loadDatas.length < res.datas.jzgQuery.totalSize){
              this.isShowLoadmore = true;
          }
          this.users = this.loadDatas;
          if(element != "loadmore"){
              this.treeData.forEach((item)=>{
                  if(item.selected){
                      item.selected = false;
                  }
              })
          }
        }else{
          this.$Message.error(res.data.msg)
        }
      })
    },
  },
  components: {
    /* eslint-disable-next-line */
  },
  mounted() {
    // this.queryOrganization();
  },
}
</script>
<style scope>
  .person-zb .antd-pe-center .ivu-radio{
    margin-bottom: 3px;
  }
</style>

